<template>
  <el-dialog
    class="ba-operate-dialog"
    :close-on-click-modal="false"
    :model-value="baTable.form.operate ? true : false"
    @close="baTable.toggleForm"
  >
    <template #header>
      <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']">
        {{ baTable.form.operate !== "edit" ? "添加" : "编辑" }}
      </div>
    </template>
    <el-scrollbar
      v-loading="baTable.form.loading"
      class="ba-table-form-scrollbar"
    >
      <div
        class="ba-operate-form"
        :class="'ba-' + baTable.form.operate + '-form'"
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
      >
        <el-form
          ref="formRef"
          @keyup.enter="baTable.onSubmit(formRef)"
          :model="baTable.form.items"
          label-position="right"
          :label-width="baTable.form.labelWidth + 'px'"
          :rules="rules"
          v-if="!baTable.form.loading"
        >
          <el-form-item label="标准名称" prop="standardName">
            <el-input
              v-model="baTable.form.items!.standardName"
              placeholder="请输入标准名称"
            />
          </el-form-item>
          <el-form-item label="巡检目的" prop="aim">
            <el-input
              v-model="baTable.form.items!.aim"
              placeholder="请输入巡检目的"
            />
          </el-form-item>
          <el-form-item label="检查项总数" prop="count">
            <el-input-number v-model="baTable.form.items!.count" :min="0" />
          </el-form-item>
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <div
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'"
      >
        <el-button @click="baTable.toggleForm('')">取消</el-button>
        <el-button
          v-blur
          :loading="baTable.form.submitLoading"
          @click="baTable.onSubmit(formRef)"
          type="primary"
        >
          {{ baTable.form.operate === "itemAdd" ? "确定并新增下一项" : "确定" }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { reactive, ref, inject, onMounted } from "vue";
import type baTableClass from "@/utils/baTable";
import type { ElForm, FormItemRule, FormRules } from "element-plus";

type Option = {
  id: number;
  label: string;
  children: Option[];
};
//接受来自父组件的注入
const baTable = inject("baTable") as baTableClass;
const formRef = ref<InstanceType<typeof ElForm>>();

const state: {
  deptOptions: Option[];
} = reactive({
  deptOptions: [], //部门列表
});
const rules = reactive<FormRules>({
  standardName: [
    { required: true, message: "标准名称不能为空", trigger: "blur" },
  ],
  aim: [{ required: true, message: "巡检目的不能为空", trigger: "blur" }],
  count: [{ required: true, message: "检查项总数不能为空", trigger: "blur" }],
});

onMounted(() => {});
</script>
<style lang="scss" scoped></style>
